package maple.util.tasks;

import java.util.Vector;

import maple.lang.Command;
import maple.lang.Language;
import maple.util.Task;

public class MasterTask extends Task
{
	public static enum Property{ GOAL, INTERVAL, LOCATION }; 

	private Vector< String > _plans;
	private Vector< String > _receivers;
	private int _state;
	private boolean _dispatched;

	/**
	 * construct a new task with the specified sender, command and receiver
	 *
	 * @param	sender		the specified sender
	 * @param	command		the specified command
	 * @param	receiver	the specified receiver
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public MasterTask( final String sender, final Command command, final String receiver )
	{
		super( sender, command, receiver );
		
		_plans = new Vector< String >();
		_receivers = new Vector< String >();
		_state = -1;
		_dispatched = false;
	}

	/**
	 * return the current plan or null if this task is not available
	 *
	 * @return	the current plan of this task 
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public String getPlan()
	{
		if( _available )
		{
			return _plans.get( _state );
		}
		return null;
	}

	/**
	 * return the current receiver for the current plan or null if this task is not available
	 *
	 * @return	the current receiver for the current plan of this task 
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public String getPlanReceiver()
	{
		if( _available )
		{
			return _receivers.get( _state );
		}
		return null;
	}

	/**
	 * return the whole plans of this task or null if this task is not available
	 *
	 * @return	the whole plans of this task
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public Vector< String > getPlans()
	{
		if( _available )
		{
			return _plans;
		}
		return null;
	}

	/**
	 * return the current state of this task or -1 if this task is not available
	 *
	 * @return	the current state
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public int getState()
	{
		if( _available )
		{
			return _state;
		}
		return -1;
	}

	/**
	 * return true if this task is dispatcheded or false if this task is not available
	 *
	 * @return	true if this task is dispatcheded
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public boolean isDispatched()
	{
		if( _available )
		{
			return _dispatched;
		}
		return false;
	}

	/**
	 * return true if this task is finished or false if this task is not available
	 *
	 * @return	true if this task is finished
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public boolean isFinished()
	{
		if( _available )
		{
			if( _state == _plans.size() )
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * return true if this task is valid 
	 *
	 * @return	true if this task is valid 
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	protected boolean isValid()
	{
		if( super.isValid() && _instruction == Language.Instruction.SET )
		{
			return true;
		}
		return false;
	}

	/**
	 * set this task dispatched
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public void setDispatched()
	{
		if( _available )
		{
			_dispatched = true;
		}
	}

	/**
	 * set the plans of this task 
	 *
	 * @param	plans	the specified plans
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public void setPlans( final Vector< String > plans )
	{
		if( _available )
		{
			for( String plan : plans )
			{
				int index = plan.indexOf( Language.DELIMITER );
				_receivers.add( plan.substring( 0, index ) );
				_plans.add( plan.substring( index + 1 ) );
			}
			_state = 0;
			_trigger_time = 0;
		}
	}

	/**
	 * set the specified state of this task
	 *
	 * @param	state	the specified state
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public void setState( final int state )
	{
		if( _available )
		{
			_state = state;
		}
	}
}
